package com.yyfax.ph.home;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * P :M: 广告通知轮播
 * Created by ying_he on 2017/12/6.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class AdNoticePresenter extends Presenter<AdNoticeModel, AdNoticePresenter.View> {
	private AdNoticePresenter.View mView;

	public static class Generator implements PresenterFactory<AdNoticePresenter> {
		@Override
		public AdNoticePresenter create() {
			return new AdNoticePresenter();
		}
	}

	@Override
	public void attach(AdNoticePresenter.View view) {
		mView = view;
		mModel = new AdNoticeModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		void getAdNoticeSuccess(List<String> adNoticeList);
		void getAdNoticeFailed();
	}

	public void getAdNotice(){
		execute(new EmptyEntity(), FUNCTIONAL_CODE, false);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getAdNoticeResult(AdNoticeModel.AdNoticeResult result) {
		if (isResponseSuccess(result)){
			if (result.getContent() != null && result.getContent().news != null){
				mView.getAdNoticeSuccess(result.getContent().news);
				return;
			}
			mView.getAdNoticeFailed();
		}else {
			mView.getAdNoticeFailed();
		}
	}
}
